import { observable, action, autorun, toJS } from 'mobx';
import { WeaTableNew, WeaForm } from 'comsMobx';

const { TableStore } = WeaTableNew;
import { ListStore } from './listStore';
import { WeaTools } from 'ecCom';
import { message, Modal } from 'antd';
import * as Apis from '../apis/mineCustomer';
import * as API_CONTACT from '../apis/contact';
import { CardManageStore } from './cardManageStore';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

export class MineCustomerStore extends ListStore {
    @observable title = '';
    @observable rightMenu = [];
    @observable searchBaseParams = {
      labelid: 'my',
    }
    @observable tabDatas = [];
    @observable sectorTreeData = [{ id: '', isLeaf: true, name: getLabel(332, '全部') + getLabel(575, '行业'), getLeafApi: '' }];
    @observable statusOptions = [{ id: '', name: getLabel(332, '全部') + getLabel(602, '状态') }];
    @observable hrmList = [];
    @observable viewTypeList = [];
    @observable quickList = [];
    @observable quickConditions = {};
    @observable contactOptions = [];
    @observable isShowResource = false;
    @observable isPro = false;
    @observable remindCount = 0;
    @observable birthdayCount = 0;
    @observable tagDatas = [];
    @observable istorefresh = 0;
    @observable showRight = false;
    @observable selectValue = '1';
    @observable rightTabData = []; // 右边栏tab
    @observable rightTabKey = '1';
    @observable cardloading = false;
    @observable form = {};
    @observable dateSelect = {
      datatype: '1',
    }
    @observable contactLogs = [];
    @observable contactLogsTotal = 0;
    @observable contacterCard = [];
    @observable contacterTotal = 0;
    @observable leaveMessage = [];
    @observable leaveMessageTotal = 0;
    @observable sellChanceCard = [];
    @observable sellChanceCardTotal = 0;
    @observable condition = [];
    @observable rightInfos = {};
    @observable cardManageStore = new CardManageStore();

    @observable hasSeasRight = false;
    @observable seasDialog = { visible: false, title: getLabel('386713', '选择公海'), contents: [], seasIds: [] };
    @observable searchsBaseKey='';

    @action
    doSearch = (params = {}) => {
      this.loading = true;
      const searchParamsQuick = toJS(this.quickConditions);

      let searchParamsAd = {};
      if (this.form.isFormInit) {
        searchParamsAd = this.form.getFormParams();
      }
      const newParams = { ...this.searchBaseParams, ...searchParamsQuick, ...searchParamsAd, ...params };
      this.searchBaseParams = { ...this.searchBaseParams, ...params };
      Apis.getMyCustomerList(newParams).then((data) => {
        if (data.status == 'success') {
          this.loading = false;
          this.tableStore.getDatas(data.sessionkey, params.current || 1);
          this.dataKey = data.sessionkey;
          this.showRight = false;

          //this.clearRightStatus();
          if(params&&params.haveContacts){
            //页面初始化时，已经搜索了联系，不需要额外加载，其他情况比如搜索了客户，则要过滤对应的联系
          }else{
            this.setRightTabKey('1', true);
          }
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }
    // 获取tab
    getMyCustomerTabs = (params = {}) => {
      Apis.getMyCustomerTags(params).then((data) => {
        if (data.status == 'success') {
          this.tabDatas = data.datas;
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }
    // 顶部栏
    conditionTop = (params = {}) => {
      this.isShowResource = false;
      const newParams = { ...this.searchBaseParams, ...params };
      Apis.getConditionTop(newParams).then((data) => {
        { data.statusOptions && data.statusOptions.unshift({ id: '', name: getLabel(332, '全部') + getLabel(602, '状态') }); }
        { data.sectorTreeData && data.sectorTreeData.unshift({ id: '', isLeaf: true, name: getLabel(332, '全部') + getLabel(575, '行业'), getLeafApi: '' }); }
        this.sectorTreeData = data.sectorTreeData;
        this.statusOptions = data.statusOptions;
        this.hrmList = data.hrmList || [];
        this.viewTypeList = data.viewTypeList || [];
        this.quickList = data.quickList || [];
        this.contactOptions = data.contactOptions || [];
        this.isShowResource = data.isShowResource || false;
      });
    }
    // 标签
    getMyCustomerTagsList = (params) => {
      Apis.getMyCustomerTagList(params).then((data) => {
        if (data.status == 'success') {
          this.tagDatas = data.datas;
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }
    // 联系提醒和生日提醒
    getRemindCount = (params) => {
      API_CONTACT.getRemindCount(params).then((data) => {
        if (data.status == 'success') {
          // console.log("data.datas.isPro",data.datas.isPro)
          this.isPro = data.datas.isPro;
          this.remindCount = data.datas.remindCount;
          this.birthdayCount = data.datas.birthdayCount;
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }
    // 标记重要
    isMarkToImportant = (params) => {
      Apis.markImportant(params).then((data) => {
        if (data.status == 'success') {
          window._table.reLoad();
          return true;
        }
        message.error(getLabel(83437, '出现错误！') + data.msgcode);
      });
    }
    // 人员信息更新
    saveHrmList = (data) => {
      this.hrmList = data;
    }
    // 行业树更新
    saveIndustry = (data) => {
      this.sectorTreeData = data;
    }
    updateParams = () => {
      this.searchBaseParams = {
        labelid: this.searchBaseParams.labelid,
      };
      this.istorefresh = this.istorefresh + 1;
      this.selectValue = '1';
      this.rightTabKey = '1';
      this.dateSelect = {
        datatype: '1',
      };
      // this.tableStore.selectedRowKeys = [];
    }
    // 标签保存
    crmTagEdit = (params) => {
      Apis.crmTagEdit(params).then((data) => {
        if (data.status == 'success') {
          message.success(getLabel('384136', '操作成功!'));
          window._table.reLoad();
          this.getMyCustomerTagsList({ type: 'crm' });
          this.getMyCustomerTabs();
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }
    // 标签删除
    crmTagDelete = (params) => {
      Apis.crmTagDelete(params).then((data) => {
        if (data.status == 'success') {
          message.success(getLabel('384136', '操作成功!'));
          window._table.reLoad();
          this.getMyCustomerTagsList({ type: 'crm' });
          this.getMyCustomerTabs();
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }
    // 应用，取消某标签
    markToCustomTagType = (params) => {
      Apis.markLabel(params).then((data) => {
        if (data.status == 'success') {
          message.success(getLabel('384136', '操作成功!'));
          window._table.reLoad();
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }
    saveSelectValue = () => {
      this.dateSelect = {
        datatype: '1',
      };
    }

    // 右侧tab切换
    setRightTabKey = (key, bool = true) => {
      this.rightTabKey = key;
      if (key == 1) {
        if (bool) {
          this.getContactLogs({ datatype: this.dateSelect.datatype });
        }
      }
      if (key == 2) {
        this.getContacterCard({ datatype: this.dateSelect.datatype, pageNum: 1, pageSize: 10 });
      }
      if (key == 3) {
        this.getLeaveMessage({ datatype: this.dateSelect.datatype });
      }
      if (key == 4) {
        this.getSellchanceCard({ datatype: this.dateSelect.datatype });
      }
    }
    // 客户联系
    @action
    getContactLogs = (params = {}) => {
      this.cardloading = true;
      const newParams = { ...this.searchBaseParams, ...this.dateSelect, ...params };
      Apis.getContactLogs(newParams).then((data) => {
        this.cardloading = false;
        this.contactLogs = data.datas.rsList;
        this.contactLogsTotal = data.datas.totalSize;
        this.dateSelect = { ...this.dateSelect, datatype: newParams.datatype };
      });
    }

    // 联系人
    getContacterCard = (params) => {
      this.cardloading = true;
      const newParams = { ...this.searchBaseParams, ...this.dateSelect, ...params };
      Apis.getContacterCard(newParams).then((data) => {
        this.cardloading = false;
        this.contacterCard = data.datas.rsList;
        this.contacterTotal = data.datas.totalSize;
        this.dateSelect = { ...this.dateSelect, datatype: newParams.datatype };
      });
    }
    // 客户留言
    getLeaveMessage = (params) => {
      this.cardloading = true;
      const newParams = { ...this.searchBaseParams, ...this.dateSelect, ...params };
      Apis.getLeaveMessage(newParams).then((data) => {
        this.cardloading = false;
        this.leaveMessage = data.datas.rsList;
        this.leaveMessageTotal = data.datas.totalSize;
        this.dateSelect = { ...this.dateSelect, datatype: newParams.datatype };
      });
    }
    // 销售机会
    getSellchanceCard = (params) => {
      this.cardloading = true;
      const newParams = { ...this.searchBaseParams, ...this.dateSelect, ...params };
      Apis.getSellchanceCard(newParams).then((data) => {
        this.cardloading = false;
        this.sellChanceCard = data.datas.rsList;
        this.sellChanceCardTotal = data.datas.totalSize;
        this.dateSelect = { ...this.dateSelect, datatype: newParams.datatype };
      });
    }
    // 我的客户 -- 点击列表 -- 客户名称s
    viewCustomerDetail = (id) => {
      this.loading = true;
      Apis.viewCustomerDetail({ customerId: id }).then((data) => {
        if (data.status == 'success') {
          this.viewCustomerDetailCallBack(data);
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }

    @action
    viewCustomerDetailCallBack = (data) => {
      this.loading = false;
      this.showRight = true;
      this.rightInfos = data.datas;
      this.cardManageStore.setTabKey('1');
    }

    clearRightStatus = () => {
      this.dateSelect = {
        datatype: '1',
      };
      this.contactLogs = [];
      this.contactLogsTotal = [];
      this.contacterCard = [];
      this.contacterTotal = 0;
      this.leaveMessage = [];
      this.sellChanceCard = [];
      this.rightInfos = {};
    }
    // 卸载
    clearStatus = () => {
      this.searchBaseParams = {
        labelid: 'my',
      };
      this.showSearchAd = false;
      this.sectorTreeData = [{ id: '', isLeaf: true, name: getLabel(332, '全部') + getLabel(575, '行业'), getLeafApi: '' }];
      this.statusOptions = [{ id: '', name: getLabel(332, '全部') + getLabel(602, '状态') }];
      this.hrmList = [];
      this.viewTypeList = [];
      this.isShowResource = false;
      this.showRight = false;
      this.selectValue = '1';
      this.rightTabKey = '1';
      this.dateSelect = {
        datatype: '1',
      };
      this.clearFormFields();
      this.istorefresh = this.istorefresh + 1;
    }

    @action
    clearFormFields=() => {
      this.form.resetConditionValue(); // 清除查询条件值
      this.searchBaseParams = {
        labelid: 'my',
      };
    }

    @action
    getSeasRight = () => {
      Apis.getSeasRight({}).then((data) => {
        if (data.datas && data.datas == 'all') {
          this.hasSeasRight = true;
        } else {
          this.hasSeasRight = false;
        }
      });
    }

    @action
    getRightSeas = () => {
      this.showSeasDialog(true);
      Apis.doJoinSeas({ operation: 'getSeas' }).then((data) => {
        this.seasDialog.contents = data.datas;
      });
    }

    @action
    showSeasDialog = (bl) => {
      this.seasDialog.visible = bl;
    }

    @action
    setSeasIds = (seasIds, datas) => {
      this.seasDialog.seasIds = seasIds;
    }

    @action
    joinSeas = () => {
      const ids = `${this.tableStore.selectedRowKeys}`;

      Apis.doJoinSeas({ ids, seasIds: `${this.seasDialog.seasIds}`, operation: 'joinSeas' }).then((data) => {
        if (data.status == '0') {
          message.error(data.errMsg);
        } else {
          message.success('加入公海成功');
          this.seasDialog.visible = false;
          this.seasDialog.seasIds = [];
          this.reLoad();
        }
      });
    }
    // 高级搜索表单
    @action
    getSearchAdForm = () => {
      Apis.getSearchCondition({ fromType: 'mimeCustomer' }).then((data) => {
        // console.log(data)
        this.condition = data.datas;
        this.searchsBaseKey = data.searchsBaseKey;
        this.form = new WeaForm();
        // 根据高级搜索条件初始化form
        this.form.initFormFields(data.datas);
      });
    }
    // 更新base输入框
    @action
    updateBase = (value) => {
      this.searchBaseParams.name = value;
      // console.log(this.searchBaseParams)
    }

    @action
    doSearchQuick = (params, quickMap) => {
      const quickSearch = quickMap.quickSearch;
      this.quickConditions[quickSearch] = params[quickSearch];
      this.quickConditions[`${quickSearch}span`] = params[`${quickSearch}span`];
      this.doSearch();
    }

    @action
    showRightChange(v) {
      this.showRight = v;
    }
}

